<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python documentation Index' />
<link rel="first" href="lib.html" title='Python library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="module-xml.dom.pulldom.html" />
<link rel="prev" href="module-xml.dom.html" />
<link rel="parent" href="markup.html" />
<link rel="next" href="dom-objects.html" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name='aesop' content='information' />
<title>8.7 xml.dom.minidom -- Lightweight DOM implementation</title>
</head>
<body>
<div class="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="8.6.3.2 accessor Methods"
  href="dom-accessor-methods.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="8. structured Markup Processing"
  href="markup.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="8.7.1 dom Objects"
  href="dom-objects.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="dom-accessor-methods.html">8.6.3.2 Accessor Methods</a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="markup.html">8. Structured Markup Processing</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="dom-objects.html">8.7.1 DOM Objects</a>
</div>
<hr /></div>
</div>
<!--End of Navigation Panel-->

<h1><a name="SECTION0010700000000000000000">
8.7 <tt class="module">xml.dom.minidom</tt> --
         Lightweight DOM implementation</a>
</h1>

<p>
<a name="module-xml.dom.minidom"></a>

<p>

<span class="versionnote">New in version 2.0.</span>

<p>
<tt class="module">xml.dom.minidom</tt> is a light-weight implementation of the
Document Object Model interface.  It is intended to be
simpler than the full DOM and also significantly smaller.

<p>
DOM applications typically start by parsing some XML into a DOM.  With
<tt class="module">xml.dom.minidom</tt>, this is done through the parse functions:

<p>
<div class="verbatim"><pre>
from xml.dom.minidom import parse, parseString

dom1 = parse('c:\\temp\\mydata.xml') # parse an XML file by name

datasource = open('c:\\temp\\mydata.xml')
dom2 = parse(datasource)   # parse an open file

dom3 = parseString('&lt;myxml&gt;Some data&lt;empty/&gt; some more data&lt;/myxml&gt;')
</pre></div>

<p>
The <tt class="function">parse()</tt> function can take either a filename or an open
file object.

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-1857' xml:id='l2h-1857' class="function">parse</tt></b>(</nobr></td>
  <td><var>filename_or_file, parser</var>)</td></tr></table></dt>
<dd>
  Return a <tt class="class">Document</tt> from the given input. <var>filename_or_file</var>
  may be either a file name, or a file-like object. <var>parser</var>, if
  given, must be a SAX2 parser object. This function will change the
  document handler of the parser and activate namespace support; other
  parser configuration (like setting an entity resolver) must have been
  done in advance.
</dl>

<p>
If you have XML in a string, you can use the
<tt class="function">parseString()</tt> function instead:

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-1858' xml:id='l2h-1858' class="function">parseString</tt></b>(</nobr></td>
  <td><var>string</var><big>[</big><var>, parser</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
  Return a <tt class="class">Document</tt> that represents the <var>string</var>. This
  method creates a <tt class="class">StringIO</tt> object for the string and passes
  that on to <tt class="function">parse</tt>.
</dl>

<p>
Both functions return a <tt class="class">Document</tt> object representing the
content of the document.

<p>
What the <tt class="function">parse()</tt> and <tt class="function">parseString()</tt> functions do
is connect an XML parser with a ``DOM builder'' that can accept parse
events from any SAX parser and convert them into a DOM tree.  The name
of the functions are perhaps misleading, but are easy to grasp when
learning the interfaces.  The parsing of the document will be
completed before these functions return; it's simply that these
functions do not provide a parser implementation themselves.

<p>
You can also create a <tt class="class">Document</tt> by calling a method on a ``DOM
Implementation'' object.  You can get this object either by calling
the <tt class="function">getDOMImplementation()</tt> function in the
<tt class="module"><a href="module-xml.dom.html">xml.dom</a></tt> package or the <tt class="module">xml.dom.minidom</tt> module.
Using the implementation from the <tt class="module">xml.dom.minidom</tt> module will
always return a <tt class="class">Document</tt> instance from the minidom
implementation, while the version from <tt class="module"><a href="module-xml.dom.html">xml.dom</a></tt> may provide
an alternate implementation (this is likely if you have the
<a class="ulink" href="http://pyxml.sourceforge.net/"
  >PyXML package</a> installed).  Once
you have a <tt class="class">Document</tt>, you can add child nodes to it to populate
the DOM:

<p>
<div class="verbatim"><pre>
from xml.dom.minidom import getDOMImplementation

impl = getDOMImplementation()

newdoc = impl.createDocument(None, "some_tag", None)
top_element = newdoc.documentElement
text = newdoc.createTextNode('Some textual content.')
top_element.appendChild(text)
</pre></div>

<p>
Once you have a DOM document object, you can access the parts of your
XML document through its properties and methods.  These properties are
defined in the DOM specification.  The main property of the document
object is the <tt class="member">documentElement</tt> property.  It gives you the
main element in the XML document: the one that holds all others.  Here
is an example program:

<p>
<div class="verbatim"><pre>
dom3 = parseString("&lt;myxml&gt;Some data&lt;/myxml&gt;")
assert dom3.documentElement.tagName == "myxml"
</pre></div>

<p>
When you are finished with a DOM, you should clean it up.  This is
necessary because some versions of Python do not support garbage
collection of objects that refer to each other in a cycle.  Until this
restriction is removed from all versions of Python, it is safest to
write your code as if cycles would not be cleaned up.

<p>
The way to clean up a DOM is to call its <tt class="method">unlink()</tt> method:

<p>
<div class="verbatim"><pre>
dom1.unlink()
dom2.unlink()
dom3.unlink()
</pre></div>

<p>
<tt class="method">unlink()</tt> is a <tt class="module">xml.dom.minidom</tt>-specific extension to
the DOM API.  After calling <tt class="method">unlink()</tt> on a node, the node and
its descendants are essentially useless.

<p>
<div class="seealso">
  <p class="heading">See Also:</p>

  <dl compact="compact" class="seetitle">
    <dt><em class="citetitle"><a href="http://www.w3.org/TR/REC-DOM-Level-1/"
        >Document Object
            Model (DOM) Level 1 Specification</a></em></dt>
    <dd>The W3C recommendation for the
            DOM supported by <tt class="module">xml.dom.minidom</tt>.</dd>
  </dl>
</div>

<p>

<p><br /></p><hr class='online-navigation' />
<div class='online-navigation'>
<!--Table of Child-Links-->
<a name="CHILD_LINKS"><strong>Subsections</strong></a>

<ul class="ChildLinks">
<li><a href="dom-objects.html">8.7.1 DOM Objects</a>
<li><a href="dom-example.html">8.7.2 DOM Example</a>
<li><a href="minidom-and-dom.html">8.7.3 minidom and the DOM standard</a>
</ul>
<!--End of Table of Child-Links-->
</div>

<div class="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="8.6.3.2 accessor Methods"
  href="dom-accessor-methods.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="8. structured Markup Processing"
  href="markup.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="8.7.1 dom Objects"
  href="dom-objects.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="dom-accessor-methods.html">8.6.3.2 Accessor Methods</a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="markup.html">8. Structured Markup Processing</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="dom-objects.html">8.7.1 DOM Objects</a>
</div>
</div>
<hr />
<span class="release-info">Release 2.5.1, documentation updated on 18th April, 2007.</span>
</div>
<!--End of Navigation Panel-->
<address>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</address>
</body>
</html>
